﻿using MicrosoftLogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace MicroCloud.Logging
{
    #region "系统启动日志"
    /// <summary>
    /// 系统启动日志
    /// </summary>
    public class StartupLogger
    {
        #region "属性"
        #region "获取 日志信息列表"
        /// <summary>
        /// 获取 日志信息列表
        /// </summary>
        public IList<LogInfo> LogInfos { get; } = new List<LogInfo>();
        #endregion

        #endregion

        #region "方法"
        #region "跟踪级别日志"
        /// <summary>
        /// 跟踪级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logName">日志名称</param>
        public void LogTrace(string message, string logName)
        {
            Log(MicrosoftLogLevel.Trace, message, logName);
        }
        #endregion
        #region "调试级别日志"
        /// <summary>
        /// 调试级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logName">日志名称</param>
        public void LogDebug(string message, string logName)
        {
            Log(MicrosoftLogLevel.Debug, message, logName);
        }
        #endregion
        #region "信息级别日志"
        /// <summary>
        /// 信息级别日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="logName">日志名称</param>
        public void LogInformation(string message, string logName)
        {
            Log(MicrosoftLogLevel.Information, message, logName);
        }
        #endregion
        #region "警告级别日志"
        /// <summary>
        /// 警告级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logName">日志名称</param>
        public void LogWarning(string message, string logName)
        {
            Log(MicrosoftLogLevel.Warning, message, logName);
        }
        #endregion
        #region "错误级别日志"
        /// <summary>
        /// 错误级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logName">日志名称</param>
        /// <param name="exception">异常</param>
        public void LogError(string message, string logName, System.Exception exception = null)
        {
            Log(MicrosoftLogLevel.Error, message, logName, exception);
        }
        #endregion
        #region "危险级别日志"
        /// <summary>
        /// 危险级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logName">日志名称</param>
        /// <param name="exception">异常</param>
        public void LogCritical(string message, string logName, System.Exception exception = null)
        {
            Log(MicrosoftLogLevel.Critical, message, logName, exception);
        }
        #endregion

        #region "跟踪级别日志"
        /// <summary>
        /// 跟踪级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logType">日志类型</param>
        public void LogTrace(string message, Type logType)
        {
            Log(MicrosoftLogLevel.Trace, message, logType);
        }
        #endregion
        #region "调试级别日志"
        /// <summary>
        /// 调试级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logType">日志类型</param>
        public void LogDebug(string message, Type logType)
        {
            Log(MicrosoftLogLevel.Debug, message, logType);
        }
        #endregion
        #region "信息级别日志"
        /// <summary>
        /// 信息级别日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="logType">日志类型</param>
        public void LogInformation(string message, Type logType)
        {
            Log(MicrosoftLogLevel.Information, message, logType);
        }
        #endregion
        #region "警告级别日志"
        /// <summary>
        /// 警告级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logType">日志类型</param>
        public void LogWarning(string message, Type logType)
        {
            Log(MicrosoftLogLevel.Warning, message, logType);
        }
        #endregion
        #region "错误级别日志"
        /// <summary>
        /// 错误级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logType">日志类型</param>
        /// <param name="exception">异常</param>
        public void LogError(string message, Type logType, System.Exception exception = null)
        {
            Log(MicrosoftLogLevel.Error, message, logType, exception);
        }
        #endregion
        #region "危险级别日志"
        /// <summary>
        /// 危险级别日志
        /// </summary>
        /// <param name="message">信息内容</param>
        /// <param name="logType">日志类型</param>
        /// <param name="exception">异常</param>
        public void LogCritical(string message, Type logType, System.Exception exception = null)
        {
            Log(MicrosoftLogLevel.Critical, message, logType, exception);
        }
        #endregion

        #region "指定级别日志"
        /// <summary>
        /// 指定级别日志
        /// </summary>
        /// <param name="logLevel">日志级别</param>
        /// <param name="message">消息内容</param>
        /// <param name="logName">日志名称</param>
        /// <param name="exception">异常</param>
        public void Log(MicrosoftLogLevel logLevel, string message, string logName, System.Exception exception = null)
        {
            LogInfo log = new() { LogLevel = logLevel, Message = message, LogType = null, LogName = logName, Exception = exception, CreatedTime = DateTime.Now };
            LogInfos.Add(log);
        }
        #endregion
        #region "指定级别日志"
        /// <summary>
        /// 指定级别日志
        /// </summary>
        /// <param name="logLevel">日志级别</param>
        /// <param name="message">消息内容</param>
        /// <param name="logType">日志类型</param>
        /// <param name="exception">异常</param>
        public void Log(MicrosoftLogLevel logLevel, string message, Type logType, System.Exception exception = null)
        {
            LogInfo log = new() { LogLevel = logLevel, Message = message, LogType = logType, LogName = "", Exception = exception, CreatedTime = DateTime.Now };
            LogInfos.Add(log);
        }
        #endregion

        #region "输出日志"
        /// <summary>
        /// 输出日志
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public void Output(IServiceProvider provider)
        {
            IDictionary<string, ILogger> dict = new Dictionary<string, ILogger>();
            foreach (LogInfo info in LogInfos.OrderBy(m => m.CreatedTime))
            {
                if (!dict.TryGetValue(info.LogName, out ILogger logger))
                {
                    logger = info.LogType != null ? provider.GetLogger(info.LogType) : provider.GetLogger(info.LogName);
                    dict[info.LogName] = logger;
                }

                switch (info.LogLevel)
                {
                    case MicrosoftLogLevel.Trace:
                        logger.LogTrace("{message}", info.Message);
                        break;
                    case MicrosoftLogLevel.Debug:
                        logger.LogDebug("{message}", info.Message);
                        break;
                    case MicrosoftLogLevel.Information:
                        logger.LogInformation("{message}", info.Message);
                        break;
                    case MicrosoftLogLevel.Warning:
                        logger.LogWarning("{message}", info.Message);
                        break;
                    case MicrosoftLogLevel.Error:
                        logger.LogError(info.Exception, "{message}", info.Message);
                        break;
                    case MicrosoftLogLevel.Critical:
                        logger.LogCritical(info.Exception, "{message}", info.Message);
                        break;
                }
            }
        }
        #endregion

        #endregion

    }
    #endregion

    #region "日志信息"
    /// <summary>
    /// 日志信息
    /// </summary>
    public class LogInfo
    {
        #region "属性"
        #region "获取或设置 日志类型"
        /// <summary>
        /// 获取或设置 日志类型
        /// </summary>
        public Type LogType { get; set; }
        #endregion
        #region "获取或设置 日志名称"
        /// <summary>
        /// 获取或设置 日志名称
        /// </summary>
        public string LogName { get; set; }
        #endregion
        #region "获取或设置 日志级别"
        /// <summary>
        /// 获取或设置 日志级别
        /// </summary>
        public MicrosoftLogLevel LogLevel { get; set; }
        #endregion
        #region "获取或设置 消息内容"
        /// <summary>
        /// 获取或设置 消息内容
        /// </summary>
        public string Message { get; set; }
        #endregion
        #region "获取或设置 异常"
        /// <summary>
        /// 获取或设置 异常
        /// </summary>
        public System.Exception Exception { get; set; }
        #endregion

        #region "获取或设置 创建时间"
        /// <summary>
        /// 获取或设置 创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; }
        #endregion

        #endregion

    }
    #endregion

}
